package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"pm_back_server/user-server/proto"
)

var userClient proto.UserClient
var conn *grpc.ClientConn

func main() {
	Init()
	TestGetUserList()
	//TestCreateUser()

	defer func(conn *grpc.ClientConn) {
		err := conn.Close()
		if err != nil {

		}
	}(conn)
}

func Init() {
	var err error
	conn, err = grpc.Dial("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic("failed to connect, err: " + err.Error())
	}

	userClient = proto.NewUserClient(conn)
}

func TestGetUserList() {
	r, err := userClient.GetUserList(context.Background(), &proto.PageInfo{PageSize: 2, PageNumber: 1})
	if err != nil {
		panic(err)
	}
	for _, user := range r.UserList {
		fmt.Println(user.Mobile, user.Nickname, user.Password)
		rsp, err := userClient.CheckPassword(context.Background(), &proto.CheckPasswordInfo{
			RawPassword:       "admin123",
			EncryptedPassword: user.Password,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println("success: ", rsp.Success)
	}
}

func TestCreateUser() {
	rsp, err := userClient.CreateUser(context.Background(), &proto.CreateUserInfo{Nickname: "hwj&mcr", Mobile: "18881006593", Password: "admin123"})
	if err != nil {
		panic(err)
	}

	fmt.Println("user id: ", rsp.UserId)
}
